package orz.itrip.mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import orz.itrip.vo.GetPreOrderVo;
import orz.itrip.vo.HotelOrderRoomVo;
import orz.itrip.vo.HotelOrderVo;
import orz.itrip.pojo.HotelOrder;
import orz.itrip.vo.PreFlushStoreVo;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;


public interface HotelOrderMapper extends Mapper<HotelOrder> {
    @Select("SELECT * FROM itrip_hotel_order WHERE itrip_hotel_order.id = #{id} AND itrip_hotel_order.userId = #{userId}")
    HotelOrderVo selectVo(HotelOrder hotelOrder);
    @Select("SELECT iho.bookingDays,iho.checkOutDate,iho.checkInDate,iho.count,iho.hotelId,iho.hotelName,iho.id,iho.linkUserName,iho.payAmount,iho.roomId,iho.specialRequirement," +
            "(SELECT ih.address FROM itrip_hotel ih WHERE iho.hotelId=ih.id) address," +
            "(SELECT ih.hotelLevel FROM itrip_hotel ih WHERE iho.hotelId=ih.id) hotelLevel," +
            "(SELECT ihr.isHavingBreakfast FROM itrip_hotel_room ihr WHERE iho.hotelId=ihr.hotelId AND ihr.id=iho.roomId) isHavingBreakfast," +
            "(SELECT ihr.roomBedTypeId FROM itrip_hotel_room ihr WHERE iho.hotelId=ihr.hotelId AND ihr.id=iho.roomId) roomBedTypeId," +
            "(SELECT ild.`name` FROM itrip_label_dic ild WHERE ild.id=(SELECT ihr.roomBedTypeId FROM itrip_hotel_room ihr WHERE iho.hotelId=ihr.hotelId AND ihr.id=iho.roomId)) roomBedTypeName," +
            "(SELECT ihr.payType FROM itrip_hotel_room ihr WHERE iho.hotelId=ihr.hotelId AND ihr.id=iho.roomId) roomPayType," +
            "(SELECT ihr.roomTitle FROM itrip_hotel_room ihr WHERE iho.hotelId=ihr.hotelId AND ihr.id=iho.roomId) roomTitle" +
            " FROM itrip_hotel_order iho WHERE iho.id = #{id} AND iho.userId = #{userId};")
    HotelOrderRoomVo selectVoRoom(HotelOrder hotelOrder);
    List<HotelOrder> selectByLike(HotelOrder hotelOrder);
    @Select("SELECT MIN(ihts.store) " +
            "FROM `itrip_hotel_temp_store` ihts WHERE ihts.roomId=#{roomId} AND ihts.hotelId=#{hotelId} " +
            "AND TO_DAYS(ihts.creationDate) BETWEEN TO_DAYS(#{checkInDate}) AND TO_DAYS(#{checkOutDate}) GROUP BY ihts.hotelId")
    Integer selectStore(GetPreOrderVo getPreOrderVo);
    @Select("SELECT store FROM itrip_product_store WHERE productId=#{roomId}")
    Integer selectProduct(GetPreOrderVo getPreOrderVo);
    @Select("SELECT ih.hotelName hotelName,ihr.roomPrice price FROM itrip_hotel_room ihr,itrip_hotel ih WHERE ih.id=#{hotelId} AND ihr.id=#{roomId}")
    GetPreOrderVo selectGetPre(GetPreOrderVo getPreOrderVo);
    @Select("SELECT SUM(count) store FROM itrip_hotel_order iho WHERE iho.roomId=#{roomId} AND iho.hotelId=#{hotelId} AND iho.orderStatus=0 " +
            "AND TO_DAYS(iho.checkInDate) BETWEEN TO_DAYS(#{checkInDate}) AND TO_DAYS(#{checkOutDate}) " +
            "AND TO_DAYS(iho.checkOutDate) BETWEEN TO_DAYS(#{checkInDate}) AND TO_DAYS(#{checkOutDate}) " +
            "GROUP BY iho.hotelId")
    Integer selectOrderScore(GetPreOrderVo getPreOrderVo);
    Integer insertOrder(HotelOrder hotelOrder);

    @Select("update itrip_hotel_order set orderStatus=1 where orderStatus=0 and TIMESTAMPDIFF(MINUTE,creationDate,NOW())>=15")
    Integer cancelAutomatically();
    @Select("update itrip_hotel_order set orderStatus=1 where orderStatus=2 and TO_DAYS(NOW())=TO_DAYS(checkOutDate)")
    Integer automaticallyCheckOut();
    @Select("call pre_flush_store(#{startTime}, #{endTime},#{roomId1}, #{hotelId1})")
    Integer selectPreFlushStore(PreFlushStoreVo preFlushStoreVo);
    @Select("UPDATE itrip_hotel_temp_store SET store=store+1 WHERE TO_DAYS(recordDate)=TO_DAYS(NOW())")
    Integer updateTempStore();
}